package com.core.web.initial;

import java.util.*;
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import com.core.configuration.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 系统参数配置类缓存装载类
 * 
 * @author 刘威
 * 
 */
public class ConfigurationLoad extends DefaultHandler {

	private static Log log = LogFactory.getLog(ConfigurationLoad.class);
	HashMap<String, String> configuration = new HashMap<String, String>();

	@Override
	public void startElement(String uri, String local, String rawName,
			Attributes attributes) throws SAXException {
		if (rawName.equals("config")) {
			String key = attributes.getValue("key");
			String value = attributes.getValue("value");
			if (key == null || value == null) {
				throw new SAXException("xml format error");
			}
			configuration.put(key, value);
		}
	}

	@Override
	public void endElement(String uri, String local, String rawName)
			throws SAXException {
	}

	/**
	 * 分析系统配置信息文件
	 * 
	 * @param FileName
	 * @param webFolder
	 *            WEB 系统路径
	 */
	public void parser(String FileName, String webFolder) {
		try {
			InputStream stream = this.getClass().getResourceAsStream(FileName);
			if (stream == null) {
				log.warn(FileName + " not found");
			}

			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser saxParser = spf.newSAXParser();
			saxParser.parse(stream, this);

			this.loadConfig(this.configuration, webFolder);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 系统配置信息装载缓存
	 * 
	 * @param config
	 * @param webFolder
	 */
	public void loadConfig(HashMap<String, String> config, String webFolder) {
		try {
			Configuration.PAGE_SIZE = Integer.parseInt(configuration
					.get("PAGE_SIZE"));
			Configuration.PAGE_INFO = configuration.get("PAGE_INFO");
			Configuration.PAGE_UNIT = configuration.get("PAGE_UNIT");
			Configuration.PAGE_FIRST = configuration.get("PAGE_FIRST");
			Configuration.PAGE_PREVIOUS = configuration.get("PAGE_PREVIOUS");
			Configuration.PAGE_NEXT = configuration.get("PAGE_NEXT");
			Configuration.PAGE_LAST = configuration.get("PAGE_LAST");
			Configuration.PAGE_TOTAL = configuration.get("PAGE_TOTAL");

			Configuration.PAGE_CSS_CURRENT = configuration
					.get("PAGE_CSS_CURRENT");
			Configuration.PAGE_CSS_LINK = configuration.get("PAGE_CSS_LINK");

			Configuration.CHOOSE_INFO = configuration.get("CHOOSE_INFO");

			Configuration.JANUARY = configuration.get("JANUARY");
			Configuration.FEBRUARY = configuration.get("FEBRUARY");
			Configuration.MARCH = configuration.get("MARCH");
			Configuration.April = configuration.get("April");
			Configuration.MAY = configuration.get("MAY");
			Configuration.JUNE = configuration.get("JUNE");
			Configuration.JULY = configuration.get("JULY");
			Configuration.AUGUST = configuration.get("AUGUST");
			Configuration.SEPTEMPBER = configuration.get("SEPTEMPBER");
			Configuration.OCTOBER = configuration.get("OCTOBER");
			Configuration.NOVEMBER = configuration.get("NOVEMBER");
			Configuration.DECEMBER = configuration.get("DECEMBER");

			Configuration.MONDAY = configuration.get("DECEMBER");
			Configuration.THURSDAY = configuration.get("THURSDAY");
			Configuration.WEDNESDAY = configuration.get("WEDNESDAY");
			Configuration.THURSDAY = configuration.get("THURSDAY");
			Configuration.FRIDAY = configuration.get("FRIDAY");
			Configuration.SATURDAY = configuration.get("SATURDAY");
			Configuration.SUNDAY = configuration.get("SUNDAY");

			Configuration.LUCENE_FOLDER = configuration.get("LUCENE_FOLDER");
			Configuration.LUCENE_SIZE = Integer.parseInt(configuration
					.get("LUCENE_SIZE"));

			Configuration.UPLOAD_FOLDER = webFolder
					+ Configuration.UPLOAD_FOLDER;

			Configuration.PROVINCE = configuration.get("PROVINCE");
			Configuration.CITY = configuration.get("CITY");

			Configuration.MAX_FEE = Double.parseDouble(configuration
					.get("MAX_FEE"));
			Configuration.HOUR_PER_CREDIT = Integer.parseInt(configuration
					.get("HOUR_PER_CREDIT"));
			Configuration.MAX_CREDIT = Integer.parseInt(configuration
					.get("MAX_CREDIT"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
